home *** CD-ROM | disk | FTP | other *** search
/ Trading on the Edge / Trading On The Edge - CD-ROM Toolkit (Wayzata Technology)(2031)(1994).bin / mac / Shared / AZTE programs / rantok.c < prev    next >
C/C++ Source or Header  |  1993-06-11  |  7KB  |  304 lines

  1. /* program to write out a token file of token values for each player in each
  2.  * round of a game, given a 'gametype' that specifies the random number
  3.  * parameters for token generation.
  4.  * R.G. Palmer 3/90.
  5.  *
  6.  * Usage:
  7.  *    Rantok nbuyers nsellers ntokens nrounds gametype
  8.  *
  9.  * 6/2/91: Modified to allow 5 digit gametypes, specifying ran5.  RGP.
  10.  * 
  11.  * 11/92: modified to link with ci.exe and create tokens file distribution
  12.  * just like dmon for use with DAD.
  13.  * 
  14.  * modified for use with hi
  15.  */
  16. #define NTRIES 50    /* Previous token.out was 50, so... */
  17. /* various limits as in protocol */
  18. #define MAXTOKENS    4    /* maximum number of tokens each */
  19. #define MAXPLAYERS    20
  20. #define MAXPRICE    8000
  21.  
  22.  
  23. /* includes and miscellaneous declarations */
  24. #include "define.h"
  25. #include <sys/types.h>
  26. #include <stdio.h>
  27. #include <string.h>
  28. #include <ctype.h>
  29. #include <time.h>
  30.  
  31. #ifndef NO_STDLIB_H
  32. #include <stdlib.h>
  33. #endif
  34.  
  35. #ifndef RAND_MAX
  36. #define RAND_MAX 2147483647
  37. #endif
  38.  
  39. extern void error();
  40.  
  41. static void randset();
  42. static void makerans();
  43. static void maketokens();
  44. static double drand();
  45. static void playertokens();
  46. static int ranrule();
  47. static void equilibrium();
  48. void rantok_main();
  49.  
  50.  
  51. /* common abbreviations and macros */
  52. #define RINT    register int
  53. #define BLOOP(i)    for (i=1; i<=nbuyers; i++)
  54. #define SLOOP(i)    for (i=1; i<=nsellers; i++)
  55.  
  56. /* basic game parameters */
  57. static int nbuyers = 0;        /* number of buyers */
  58. static int nsellers = 0;        /* number of sellers */
  59. static int gametype = 0;        /* type of game */
  60. static int nrounds = 0;        /* maximum number of rounds */
  61. static int ntokens = 4;        /* number of tokens for each player */
  62. static int ran1 = 0;            /* parameter 1 for token choice */
  63. static int ran2 = 0;            /* parameter 2 for token choice */
  64. static int ran3 = 0;            /* parameter 3 for token choice */
  65. static int ran4 = 0;            /* parameter 4 for token choice */
  66. static int ran5 = 0;            /* parameter 5 for token choice */
  67. static int ndemand;            /* for computing equilibrium.    */
  68. static int nsupply;            /* "                 */
  69. static int supply[MAXTOKENS*MAXPLAYERS];
  70. static int demand[MAXTOKENS*MAXPLAYERS];
  71.  
  72. /* working variables */
  73. static int r = 0;            /* round number */
  74.  
  75. static int buyer[MAXPLAYERS][MAXTOKENS];
  76. static int seller[MAXPLAYERS][MAXTOKENS];
  77.  
  78. void rantok_main(a,b,c,d,e)
  79. int a,b,c,d,e;
  80. {
  81.     FILE *fd;
  82.  
  83. /* set up random number generator */
  84.     randset();
  85.  
  86. /* get parameters */
  87.     nbuyers = d;
  88.     nsellers = e;
  89.     ntokens = c;
  90.     nrounds = b;
  91.     gametype = a;
  92.  
  93. /* make ran1-ran5 */
  94.     makerans();
  95.  
  96. /* Open file tokens.out and write out the sample data (deletes previous info)*/
  97.     if ((fd = fopen("tokens.out","w")) == NULL)
  98.     error("Can't open tokens.out for writing");
  99.  
  100. /* Header info for tokens.out */
  101.     fprintf(fd,"%d %d %d %d %d\n", gametype, ntokens, nbuyers, nsellers,
  102.     NTRIES);
  103.  
  104. /* make NTRIES sample distributions */
  105.     for (r=1; r<=NTRIES; r++)
  106.     maketokens(fd);
  107.  
  108.     fclose(fd);
  109. }
  110.  
  111. static void
  112. maketokens(fd)    /* Makes and writes out ntokens for each player */
  113. FILE *fd;
  114. {
  115.     RINT i;
  116.     double drand();
  117.     int offset,boffset,soffset;
  118.     int btok[MAXTOKENS],stok[MAXTOKENS];
  119.  
  120.     offset = (int)(ran1*drand());
  121.     boffset = offset + (int)(ran2*drand());
  122.     soffset = offset;
  123.     for (i=0; i<ntokens; i++) {
  124.     btok[i] = boffset + (int)(ran3*drand());
  125.     stok[i] = soffset + (int)(ran3*drand());
  126.     }
  127.  
  128.     BLOOP(i)
  129.     playertokens(i,btok,1,fd);
  130.     SLOOP(i)
  131.     playertokens(i,stok,0,fd);
  132.  
  133. /* print out the tokens and equilibrium price & trades */
  134.     equilibrium(fd);
  135. }
  136.  
  137. static void
  138. playertokens(player,base,isbuyer,fd)
  139. int player,base[],isbuyer;
  140. FILE *fd;
  141. /* make one player's tokens */
  142. {
  143.     RINT j,i;
  144.     int tok,toks[MAXTOKENS];
  145.     double drand();
  146.     int playeroffset;
  147.  
  148. /* offset for each player, same for all tokens */
  149.     playeroffset = (int)(ran5*drand());
  150.  
  151. /* make tokens */
  152.     for (j=0; j<ntokens; j++) {
  153.     toks[j] = playeroffset + base[j] + (int)(ran4*drand());
  154.     if (toks[j]>MAXPRICE)
  155.         toks[j] = MAXPRICE;
  156.     }
  157. /* sort into increasing order, straight insertion */
  158.     for (j=1; j<ntokens; j++) {
  159.     tok = toks[j];
  160.     i = j-1;
  161.     while (i>=0 && toks[i]>tok) {
  162.         toks[i+1] = toks[i];
  163.         i--;
  164.     }
  165.     toks[i+1] = tok;
  166.     }
  167.  
  168. /* copy into player structure in decreasing order if buyer, increasing
  169.  * if seller */
  170.     if (isbuyer)
  171.     for (i=0; i<ntokens; i++)
  172.         buyer[player][i] = toks[ntokens-1-i];
  173.     else 
  174.     for (i=0; i<ntokens; i++)
  175.         seller[player][i] = toks[i];
  176.  
  177. /* write out in decreasing order if buyer, increasing if seller */
  178. /*    fprintf(fd,"%c %d",(buyer)?'B':'S',r);
  179.     for (i=0; i<ntokens; i++)
  180.     fprintf(fd," %4d", (isbuyer)? toks[ntokens-1-i]: toks[i]);
  181.     fprintf(fd,"\n");
  182. */
  183. }
  184.  
  185. static void
  186. makerans()    /* calculate ran1-ran4 from gametype */
  187. {
  188.     ran5 = ranrule(gametype/10000);
  189.     ran1 = ranrule((gametype%10000)/1000);
  190.     ran2 = ranrule((gametype%1000)/100);
  191.     ran3 = ranrule((gametype%100)/10);
  192.     ran4 = ranrule(gametype%10);
  193.  
  194. }
  195.  
  196.  
  197. static int
  198. ranrule(k)    /* computes ran(k) = 3**k - 1 */
  199. int k;
  200. {
  201.     int i = 1;
  202.     if (k > 8) error("illegal digit 9 in gametype");
  203.     if (k > 8) k = 8;
  204.     while (k--) i *= 3;
  205.     return (i-1);
  206. }
  207.  
  208. static void
  209. randset()
  210. /*
  211.  * Set up random number generator
  212.  */
  213. {
  214.     time_t time();
  215.     double drand();
  216.     unsigned int seed;
  217.     int i;
  218.  
  219.     seed = (unsigned int) time((time_t *)0);
  220.     srand((int)(seed&RAND_MAX));
  221.     for (i=0; i<13; i++)
  222.     drand();        /* exercise it */
  223. }
  224.  
  225.  
  226. static double
  227. drand()
  228. /*
  229.  * Random number generator, uniform in 0.0 -- 1.0
  230.  */
  231. { return ((double) rand())/(((double) RAND_MAX) + 1.0); }
  232.  
  233. static void equilibrium(fd)
  234. FILE *fd;
  235. {
  236.     RINT k,i,j;
  237.     int tok,surp;
  238.     int eqm,eqmtrades,eqmupper,eqmlower;
  239.     int id[MAXTOKENS*MAXPLAYERS];
  240.  
  241.     if (nbuyers==0 || nsellers==0)    /* can't happen */
  242.     error("in equilibrium");
  243.  
  244. /* sort all the buyer's tokens into decreasing order */
  245.     ndemand = 0;
  246.     for (j=0; j< ntokens; j++) {
  247.     BLOOP(i) {
  248.         tok = buyer[i][j];
  249.         k = ndemand-1;
  250.         while (k>=0 && demand[k]<tok) {
  251.         demand[k+1] = demand[k];
  252.         id[k+1] = id[k];
  253.         k--;
  254.         }
  255.         demand[k+1] = tok;
  256.         id[k+1] = i;
  257.         ndemand++;
  258.     }
  259.     }
  260.  
  261. /* sort all the seller's tokens into increasing order */
  262.     nsupply = 0;
  263.     for (j=0; j<ntokens; j++) {
  264.     SLOOP(i) {
  265.         tok = seller[i][j];
  266.         k = nsupply-1;
  267.         while (k>=0 && supply[k]>tok) {
  268.         supply[k+1] = supply[k];
  269.         id[k+1] = id[k];
  270.         k--;
  271.         }
  272.         supply[k+1] = tok;
  273.         id[k+1] = i;
  274.         nsupply++;
  275.     }
  276.     }
  277.  
  278. /* find the equilibrium #trades, price range, and surplus */
  279.     surp = 0;
  280.     for (k=0; k<nsupply && k<ndemand && demand[k]>supply[k]; k++)
  281.     surp += demand[k]-supply[k];
  282.     eqmtrades = k;
  283.     if (k > 0) {
  284.     eqmupper = demand[k-1];
  285.     if (k<nsupply && supply[k]<eqmupper) eqmupper = supply[k];
  286.     eqmlower = supply[k-1];
  287.     if (k<ndemand && demand[k]>eqmlower) eqmlower = demand[k];
  288.     eqm = (eqmlower+eqmupper)/2;
  289.     }
  290.     else {
  291.     eqmlower = MAXPRICE+1;
  292.     eqmupper = -1;
  293.     eqm = -1;
  294.     }
  295.  
  296. /* Print out a line to the tokens.out file */
  297.     for(j=0;j<ndemand;j++)
  298.         fprintf(fd," %4d",demand[j]);
  299.     for(j=0;j<nsupply;j++)
  300.         fprintf(fd," %4d",supply[j]);
  301.     fprintf(fd," %4d %4d\n",eqm,eqmtrades);
  302.  
  303. } /* equilibrium */
  304.